package com.company.project.modular.standard.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.base.tips.ErrorTip;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.model.Audit;
import com.company.project.modular.system.model.Instock;
import com.company.project.modular.system.service.IAuditService;
import com.company.project.modular.system.service.IInstockService;
import com.company.project.modular.system.service.IShareService;
import com.company.project.modular.system.model.Share;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.warpper.ShareWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 对照品共享控制器
 *
 * @author fengshuonan
 * @Date 2018-10-30 15:42:52
 */
@Controller
@RequestMapping("/share")
public class ShareController extends BaseController {

    private String PREFIX = "/standard/share/";

    @Reference
    private IShareService shareService;
    @Reference
    private IStandardService standardService;
    @Reference
    private IInstockService instockService;
    @Reference
    private IAuditService auditService;
    /**
     * 跳转到对照品共享首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "share.html";
    }

    /**
     * 跳转到添加对照品共享
     */
    @RequestMapping("/share_add")
    public String shareAdd() {
        return PREFIX + "share_add.html";
    }

    /**
     * 跳转到修改对照品共享
     */
    @RequestMapping("/share_update/{shareId}")
    public String shareUpdate(@PathVariable Integer shareId, Model model) {
        Share share = shareService.selectById(shareId);
        model.addAttribute("item",share);
        LogObjectHolder.me().set(share);
        return PREFIX + "share_edit.html";
    }
    /**
     * 获取标准物质表里面的标识码standardCode
     *
     */
    @RequestMapping("/shareStandard")
    @ResponseBody
    public List<String> shareStandard(Model model) {
        /*查询标准物质表里面的标识码*/
        List<String> shareStandard = standardService.findByStandarCode();
        /*查询标准物质表里面的名称*/
        /*List<String> shareStandard = standardService.findByStandardName();*/
        return shareStandard;
    }

    /**
     * 获取对照品共享列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String shareState) {
        /*shareService.selectMaps(null);*/
        List<Map<String, Object>> share=shareService.list(condition,shareState);
        return new ShareWarpper(share).warp();
    }

    /**
     * 新增对照品共享
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Share share, Instock instock, Audit audit, BindingResult bindingResult) {
        /*共享编号*/
        List<String> maxNumber1 = shareService.findByMaxNumber();
        if (maxNumber1.size() == 0) {
            int a = 700001;
            String number = String.valueOf(a);
            share.setShareCode(number);
        } else {
            String maxNumber = maxNumber1.get(0);
            int a = Integer.valueOf(maxNumber) + 1;
            String number = String.valueOf(a);
            share.setShareCode(number);
        }

        /*共享数量的计算，根据标识码和对应标识码的出入库总次数standardFrequency （也就是需要查询对应标识码的最大的出入库总次数）去查询库存表里面的剩余量standardNumber
         * 即1.根据标识码去查询最大出入库次数，2.根据标识码和最大出入库次数去查余量*/
        String standardCode = share.getShareStandard();//获取共享标识符
        List<String> maxF = instockService.findByMaxF(standardCode);
        if (maxF.size() == 0) {
            System.out.println("获取到最大的次数列表为空");
            return new ErrorTip(10, "添加失败!该物质没有余量!");
        } else {
        String maxTime = maxF.get(0);   //获取到最大的次数
        List<String> StandardNumber = instockService.findByStandardNumber(standardCode, maxTime);//查询剩余量
        /* System.out.println("StandardNumber"+StandardNumber);*/
        int StandardNumber1 = Integer.valueOf(StandardNumber.get(0));
        double standardNumber2 = share.getShareNumber();  //页面输入的关共享数量
        if (StandardNumber1 - standardNumber2 > 10) {
            double standardNumber3 = StandardNumber1 - standardNumber2;//最终剩余量
            /*然后还要添加一条库存记录*/
            instock.setStandardNumber(standardNumber3);
            /*出库次数加1*/
            List<String> outFrequencyList = instockService.findByOutFrequency(standardCode);
            if (outFrequencyList.size() == 0) {
                instock.setStandardOutFrequency(1);
            } else {
                String outFrequency = outFrequencyList.get(0);
                int out = Integer.valueOf(outFrequency) + 1;
                instock.setStandardOutFrequency(out);
            }
            int standard_frequency = Integer.valueOf(maxTime) + 1;//总次数+1
            instock.setStandardFrequency(standard_frequency);

            instock.setStandardCode(share.getShareStandard());//标识码
            //得到long类型当前时间
            long l = System.currentTimeMillis(); //new日期对象
            Date date = new Date(l);
            /*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*///转换提日期输出格式
            instock.setStandardOutDate(date);
            instock.setStandardOutNumber(share.getShareNumber());
            instockService.insert(instock);

            share.setShareState(0);//领取状态--未领取
            shareService.insert(share);

            /*添加审核表的信息*/
            String maxNumberAudit = String.valueOf(auditService.findByMaxNumber().get(0).getAuditId());
            /*审核单号*/
            if ("null".equals(maxNumberAudit)) {
                int a = 30001;
                String number = String.valueOf(a);
                audit.setAuditId(number);
            } else {
                int a = Integer.valueOf(maxNumberAudit) + 1;
                String number = String.valueOf(a);
                audit.setAuditId(number);
            }
            /*将buyId存到auditPurId里面*/
            if (maxNumber1.size() == 0) {
                audit.setAuditPurId("70001");
            } else {
                String maxNumber = maxNumber1.get(0);
                int a = Integer.valueOf(maxNumber) + 1;
                String number = String.valueOf(a);
                audit.setAuditPurId(number);
            }
            audit.setAuditPersion("等待审核");
            audit.setAuditPass(3);  //未审核
            audit.setAuditState(3);//兄弟单位
            String StandardCode = share.getShareStandard();
            String auditName = standardService.findByAuditName(StandardCode);
            audit.setAuditName(auditName);
            auditService.insert(audit);
            /*return SUCCESS_TIP;*/
        } else {
            return new ErrorTip(0, "余量不足，不能申请，剩下的余量为：" + StandardNumber1);
        }
    }
        return SUCCESS_TIP;
        /*return new ErrorTip(200, "添加成功");*/
    }

    /**
     * 删除对照品共享
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer shareId) {
        shareService.deleteById(shareId);
        return SUCCESS_TIP;
    }

    /**
     * 修改对照品共享
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Share share,BindingResult bindingResult) {
        shareService.updateById(share);
        return SUCCESS_TIP;
    }

    /**
     * 对照品共享详情
     */
    @RequestMapping(value = "/detail/{shareId}")
    @ResponseBody
    public Object detail(@PathVariable("shareId") Integer shareId) {
        return shareService.selectById(shareId);
    }
}
